package cn.kgc.day05.util;

import cn.hutool.json.JSONUtil;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @author 课工场
 * @date 2024/10/15
 * @description
 */
public class ResponseUtil {

    public static void  writeJson(Object data, HttpServletResponse resp){
        // 响应json格式数据     html
        resp.setContentType("application/json;charset=utf-8");
        // responseResult java对象  ->  json格式的字符串
        String jsonStr = JSONUtil.toJsonStr(data);
        PrintWriter writer = null;
        try {
            writer = resp.getWriter();
            writer.write(jsonStr);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }finally {
            writer.close();
        }
    }
}
